YAHOO.namespace("springmole.FormElement");

/**
 * 
 * @param fieldElementArray
 */
YAHOO.springmole.FormElement = function (fieldElementArray) {
	var elementArray = fieldElementArray ? fieldElementArray : [];

	/**
	 * 
	 * @param formName
	 * @param formAction
	 */
	function buildForm(formName, formAction) {
		var form,
			cnt,
			fieldId,
			rowDiv,
			label,
			inputType;
		
		form = new YAHOO.util.Element(document.createElement('form'));
		form.set('id', formName);
		form.set('name', formName);
		form.set('method', 'POST');
		form.set('action', formAction);

		for (cnt = 0; cnt < elementArray.length; cnt++) {
			fieldId = elementArray[cnt].get('id');

			rowDiv = new YAHOO.util.Element(document.createElement('div'));
			inputType = elementArray[cnt].get('type');
			
			// Don't append label element if the field is <input type="hidden"/>
			if (inputType && inputType !== 'hidden') {
				label = new YAHOO.util.Element(document.createElement('label'));
				label.set('for', fieldId);
				label.get('element').innerHTML = fieldId;
				label.appendTo(rowDiv);
			}
			
			elementArray[cnt].appendTo(rowDiv);

			rowDiv.appendTo(form);
		}

		return form;
	}
	

	/**
	 * 
	 * @param id
	 * @param optionArray
	 */
	function addSelect(id, optionArray) {
		var selectElement,
			cnt,
			optionElement;
		
		selectElement = new YAHOO.util.Element(document.createElement('select'));
		selectElement.set('id', id);
		selectElement.set('name', id);

		for (cnt = 0; cnt < optionArray.length; cnt++) {
			optionElement = new YAHOO.util.Element(document.createElement('option'));
			optionElement.set('value', optionArray[cnt].value);
			optionElement.get('element').innerHTML = optionArray[cnt].name;
			optionElement.appendTo(selectElement);
		}

		elementArray.push(selectElement);
	}
	

	/**
	 * 
	 * @param id
	 * @param type
	 * @param value
	 */
	function addInput(id, type, value) {
		var inputElement = new YAHOO.util.Element(document.createElement('input'));
		inputElement.set('id', id);
		inputElement.set('name', id);
		inputElement.set('type', type);
		inputElement.set('value', value);
		elementArray.push(inputElement);
	}
	

	return {
		buildForm : buildForm,
		addInput : addInput,
		addSelect : addSelect
	};

};
